package com.micosco.taskremind.data;

import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.Ignore;
import androidx.room.PrimaryKey;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Objects;

@Entity(tableName = "Tasks")
public class Task {
    @PrimaryKey(autoGenerate = true)
    private int uID;//uID由Room自动生成。
    @ColumnInfo(name = "Title")
    private String title = "";
    @ColumnInfo(name = "Subject")
    private String subject = "";
    @ColumnInfo(name = "Detail_Content")
    private String detailContent = "";
    @ColumnInfo(name = "Finished")
    private boolean finished = false;
    @ColumnInfo(name = "Start_Time")
    private LocalDate start;
    @ColumnInfo(name = "Deadline")
    private LocalDateTime deadline;
    @ColumnInfo(name = "Last_Modify_Time")
    private LocalDateTime lastModify;
    @ColumnInfo(name = "Remind_Type")
    private RemindType remindType;

    @Ignore
    private static int maxID = 0;

    public Task() {
    }

    @Ignore
    public Task(String title,
                String subject,
                String detailContent,
                LocalDate start,
                LocalDateTime deadline,
                boolean finished) {
        this.uID = ++maxID;
        this.title = title;
        this.subject = subject;
        this.detailContent = detailContent;
        this.finished = finished;
        this.start = start;
        this.deadline = deadline;
    }

    @Ignore
    public Duration remainingTime() {
        LocalDateTime now = LocalDateTime.now();
        return Duration.between(now, deadline);
    }

    @Ignore
    public boolean isOverdue() {
        return LocalDateTime.now().isAfter(deadline);
    }

    @Ignore
    public String remainingTimeString() {
        Duration remainingTime = remainingTime();
        if (remainingTime.toDays() > 0) {
            return remainingTime.toDays() + " 天";
        } else if (remainingTime.toHours() > 0) {
            return remainingTime.toHours() + " 小时";
        } else if (remainingTime.toMinutes() > 0) {
            return remainingTime.toMinutes() + " 分";
        } else if (remainingTime.getSeconds() > 0) {
            return remainingTime.getSeconds() + " 秒";
        } else {
            return "已逾期";
        }
    }


    //getters
    public int getUID() {
        return uID;
    }

    public String getTitle() {
        return title;
    }

    public String getSubject() {
        return subject;
    }

    public String getDetailContent() {
        return detailContent;
    }

    public boolean isFinished() {
        return finished;
    }

    public LocalDate getStart() {
        return start;
    }

    public LocalDateTime getDeadline() {
        return deadline;
    }

    public LocalDateTime getLastModify() {
        return lastModify;
    }

    public RemindType getRemindType() {
        return remindType;
    }

    //setters
    public void setUID(int uID) {
        this.uID = uID;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public void setDetailContent(String detailContent) {
        this.detailContent = detailContent;
    }

    public void setFinished(boolean finished) {
        this.finished = finished;
    }

    public void setDeadline(LocalDateTime deadline) {
        this.deadline = deadline;
    }

    public void setLastModify(LocalDateTime lastModify) {
        this.lastModify = lastModify;
    }

    public void setStart(LocalDate start) {
        this.start = start;
    }

    public void setRemindType(RemindType remindType) {
        this.remindType = remindType;
    }

    @Ignore
    public static void setMaxID(int maxID) {
        if (maxID > Task.maxID) {
            Task.maxID = maxID;
        }
    }

    @Override
    @Ignore
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Task task = (Task) o;
        return uID == task.uID &&
                finished == task.finished &&
                Objects.equals(title, task.title) &&
                Objects.equals(subject, task.subject) &&
                Objects.equals(detailContent, task.detailContent) &&
                Objects.equals(start, task.start) &&
                Objects.equals(deadline, task.deadline) &&
                Objects.equals(lastModify, task.lastModify);
    }

    @Override
    @Ignore
    public int hashCode() {
        return Objects.hash(uID, title, subject, detailContent, finished, start, deadline,
                lastModify);
    }


}
